package Api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class SimpleArrayList {
    private Object[] data;
    private int size;

    public SimpleArrayList() {
        this(16);
    }

    public SimpleArrayList(int cap) {
        data = new Object[cap];
    }

    public void add(Object element) {
        if (size == data.length) {
            data = Arrays.copyOf(data, 2 * data.length);
        }
        data[size] = element;
        size++;
    }

    public boolean remove(Object obj) {
        if (size == 0) return false;
        boolean flag = false;
        for (int i = 0; i < size; i++) {
            if (data[i].equals(obj)) {
                System.arraycopy(data, i + 1, data, i, size - i - 1);
                flag = true;
                break;
            }
        }
        if (!flag) return false;
        data[size - 1] = null;
        size--;
        return true;
    }

    public int size() {
        return size;
    }

    public String toString(){
        StringBuilder sb = new StringBuilder("[");
        for(int i = 0; i<size;i++) {
        sb.append(data[i]).append(",");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append("]");
        return sb.toString();
    }

    public void add (int index, Object element){
            if (index > size);
            System.arraycopy(data, index, data, index + 1, size - index);
            data[index] = element;
            size++;
    }
    public boolean contains(Object element){
        for (int i=0;i<size;i++){
            if (element.equals(data[i])){
                return true;
            }
        }
        return false;

    }

    public static void main (String[]args){
            SimpleArrayList a = new SimpleArrayList();
            a.add(200);
            a.add(200);
            a.add(0, 100);
            System.out.println(a);
            System.out.println(a.contains(100));
        }
}
